package com.mzy.linear_struct.面试150题;

import java.util.Deque;
import java.util.LinkedList;

/**
 * @Author: mizy3
 * @Date: 2024/08/16/下午3:33
 * @Description:
 */
public class Pro20有效的括号 {
    Deque<Character> stack = new LinkedList<>();
    public boolean isValid(String s) {
        char[] sarr = s.toCharArray();

        for (int i = 0; i < sarr.length; i++) {
            if (sarr[i] == '(') {
                stack.push(')');
            }else if(sarr[i] == '['){
                stack.push(']');
            }else if(sarr[i] == '{'){
                stack.push('}');
            }else {
                if(!stack.isEmpty() && stack.pop() != sarr[i]){
                    return false;
                }
            }
        }

        return true;
    }
}
